package com.bawei.a2010douyin.ui.fragment;

import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.bawei.a2010douyin.R;
import com.bawei.a2010douyin.adapter.MyFragmentPagerAdapter;
import com.bawei.a2010douyin.base.mvp.BaseFragment;
import com.bawei.a2010douyin.entity.MyTabentity;
import com.bawei.a2010douyin.ui.fragment.home.GuanZhuFragment;
import com.bawei.a2010douyin.ui.shopping.ShoppingFragment;
import com.bawei.a2010douyin.ui.tuijian.TuiJianFragment;
import com.flyco.tablayout.CommonTabLayout;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;


public class HomeFragment extends BaseFragment {
    private ViewPager viewPager;
    private CommonTabLayout common;
    private ArrayList<CustomTabEntity> mTabEntitys = new ArrayList<>();
    private List<Fragment> mList = new ArrayList<>();
    private MyFragmentPagerAdapter mMyFragmentPagerAdapter;



    @Override
    public int bindLayout() {
        return R.layout.fragment_home;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        viewPager = (ViewPager) findViewById(R.id.view_pager);
        common = (CommonTabLayout) findViewById(R.id.common);
        mTabEntitys.clear();
        mList.clear();
        //设置tab数据
        mTabEntitys.add(new MyTabentity(getString(R.string.tab_guanzhu),0,0));
        mTabEntitys.add(new MyTabentity(getString(R.string.tab_shopping),0,0));
        mTabEntitys.add(new MyTabentity(getString(R.string.tab_tuijian),0,0));
        common.setTabData(mTabEntitys);
        //viewpager
        mList.add(new GuanZhuFragment());
        mList.add(new ShoppingFragment());
        mList.add(new TuiJianFragment());
        mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getChildFragmentManager(), FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT,mList);
        viewPager.setAdapter(mMyFragmentPagerAdapter);
        //上下联动
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                common.setCurrentTab(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        common.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                viewPager.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });




    }

    @Override
    public void initData() {

    }
}